package com.woniuxy.jdkproxy;

import com.woniuxy.aspect.MyAspect;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 *@BelongsProject: java-85-course-code
 *@BelongsPackage: com.woniuxy.jdkproxy
 *@Author: LiTongMiao
 *@Date: 2022/1/20 12:21
 *@Description: TODO
 */
public class JdkProxyFactory {
    public static Object getProxyObject(Object targetClass){
        return Proxy.newProxyInstance(targetClass.getClass().getClassLoader(),targetClass.getClass().getInterfaces(),(Object proxy, Method method, Object[] args)->{
            //前置增强
            MyAspect myAspect = new MyAspect();
            myAspect.beforeLog();
            //目标方法
            Object invoke = method.invoke(targetClass, args);
            myAspect.afterLog();
            return invoke;
        });
    }
}